import requests
import cv2
import json
import numpy as np
import base64
from io import BytesIO
def get_points68(img):
    # url = "http://47.115.89.81:8086/get_landmarks_b64_68/"
    url = "http://localhost:8086/get_landmarks_b64_68/"

    # 定义请求头
    headers = {
        "accept": "application/json",
    }
    result=None
    # 构造 multipart/form-data 请求
    try:
        _, buffer = cv2.imencode('.jpg', img)  # 假设我们将其编码为JPEG格式
        byte_arr = np.array(buffer)

        # 或者，可以直接使用BytesIO进行内存操作
        # byte_io = io.BytesIO()
        # cv2.imencode('.jpg', image)[1].tofile(byte_io)
        # byte_arr = byte_io.getvalue()

        # Step 3: 将字节流编码为base64字符串
        encoded_img = base64.b64encode(buffer)
        # 返回base64编码的字符串
        base64_str= encoded_img.decode('utf-8')  # 为了方便处理，通常会解码回文本格式
        data = json.dumps({
            "image": base64_str
        })

        response = requests.post(
            url, data=data, headers=headers
        )

        # 检查响应状态码
        if response.status_code == 200:
            print("请求成功！")
            result=response.json()
            # print("响应内容（JSON）:", response.json())
        else:
            print(f"请求失败，状态码: {response.status_code}")
            print("错误信息:", response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求异常: {e}")
    return result
if __name__=="__main__":
    pass
    # image="./data/face-male.jpg"
    # img=cv2.imread(image)
    #
    # result=get_points68(img)
    # idx = 0
    # for solexpot in result["landmarks"]:
    #     if i>
    #     x1, y1 = solexpot
    #     # cv2.circle(img, (int(x1), int(y1)), 3, (255, 0, 0), -1)
    #     cv2.putText(img, str(idx), (int(x1), int(y1)), cv2.FONT_HERSHEY_COMPLEX, 0.3, (255, 0, 0), 1)
    #
    # cv2.imshow("copy_img", img)
    #
    # cv2.waitKey()